package com.dream.keigezhushou.Activity;


import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.dream.keigezhushou.Activity.Uiutils.UiUtils;
import com.dream.keigezhushou.Activity.bean.SeachInfo;
import com.dream.keigezhushou.Activity.wigdt.XCFlowLayout;
import com.dream.keigezhushou.R;

import java.util.ArrayList;
import java.util.List;

public class SeachActivity extends BaseActivity implements View.OnClickListener {


    private String mNames[] = {
            "购物", "美食", "游玩", "北京", "CSDN", "Airsaid", "周游", "新春送祝福"};
    private XCFlowLayout mFlowLayout;
    private List<SeachInfo> mList = new ArrayList<>();
    private ImageView mIvSeach;
    private LinearLayout mMLlSeach;
    private TextView mMSeachTv;
    private TextView mMTvHottitle;
    private XCFlowLayout mHotFlowLayout;
    private TextView mMTvHistory;
    private TextView mMTvClean;
    private ListView mLvList;
    private LinearLayout mMActivitySeach;
    private EditText mEttext;
    private LvAdapter mLvAdapter;
    private LvAdapter mMLvAdapter;

    // private String[] mNames= new String[]{"小苹果","小酒窝","小芳","无语","悟","流星雨"};


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seach);

        Window window = getWindow();
        //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        //设置状态栏颜色
        window.setStatusBarColor(getResources().getColor(R.color.primary));

        ViewGroup mContentView = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
        View mChildView = mContentView.getChildAt(0);
        if (mChildView != null) {
            //注意不是设置 ContentView 的 FitsSystemWindows, 而是设置 ContentView 的第一个子 View . 预留出系统 View 的空间.
            ViewCompat.setFitsSystemWindows(mChildView, true);
        }

        initview();
        initChildViews();
        initData();
        mMLvAdapter = new LvAdapter();
        mLvList.setAdapter(mMLvAdapter);


        //判断历史记录是否为空
        //为空不显示 不为空则显示
    }

    @Override
    protected void onStart() {
        super.onStart();
        mMLvAdapter.notifyDataSetChanged();
        if (mList.size() > 0) {
            mLvList.setVisibility(View.VISIBLE);
        } else {
            mLvList.setVisibility(View.GONE);
        }
    }

    private void initview() {
        mIvSeach = (ImageView) findViewById(R.id.iv_seach);

        mMLlSeach = (LinearLayout) findViewById(R.id.ll_seach);
        mMLlSeach.setOnClickListener(this);
        mMSeachTv = (TextView) findViewById(R.id.seach_tv);
        mMSeachTv.setOnClickListener(this);
        mMTvHottitle = (TextView) findViewById(R.id.tv_hottitle);

        mHotFlowLayout = (XCFlowLayout) findViewById(R.id.hot_flowLayout);

        mMTvHistory = (TextView) findViewById(R.id.tv_history);

        mMTvClean = (TextView) findViewById(R.id.tv_clean);
        mMTvClean.setOnClickListener(this);
        mLvList = (ListView) findViewById(R.id.lv_list);
        mMActivitySeach = (LinearLayout) findViewById(R.id.activity_seach);
        mEttext = (EditText) findViewById(R.id.et_text);
        //输入框监听


    }

    private void initData() {
        SeachInfo xpg = new SeachInfo("小苹果");
        SeachInfo xjw = new SeachInfo("小酒窝");
        SeachInfo xf = new SeachInfo("小芳");
        SeachInfo wy = new SeachInfo("无语");
        SeachInfo wu = new SeachInfo("悟");
        SeachInfo lxy = new SeachInfo("流星雨");
        mList.add(xpg);
        mList.add(xjw);
        mList.add(xf);
        mList.add(wy);
        mList.add(wu);
        mList.add(lxy);

    }

    private void initChildViews() {
        // TODO Auto-generated method stub
        mFlowLayout = (XCFlowLayout) findViewById(R.id.hot_flowLayout);
//
        ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        lp.leftMargin = 5;
        lp.rightMargin = 8;
        lp.topMargin = 5;
        lp.bottomMargin = 8;
        for (int i = 0; i < mNames.length; i++) {
            final TextView view = new TextView(this);
            view.setText(mNames[i]);
            view.setTextColor(Color.BLACK);
            view.setBackgroundDrawable(getResources().getDrawable(R.drawable.lable_item_bg_nomal));
            view.setTag(i);
            view.setTextSize(13);
            //  view.setBackgroundResource(R.drawable.lable_item_bg_nomal);
            mFlowLayout.addView(view, lp);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), mNames[(int) view.getTag()], Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.et_text:
                //edittext 监听
                UiUtils.toast("edittext 监听");
                //列出相关歌曲供选择
                break;
            case R.id.seach_tv:
                UiUtils.toast("搜索歌曲");
                String name = mEttext.getText().toString().trim();
                 mList.add(toBean(name));
                mMLvAdapter.notifyDataSetChanged();

                //查找展示相关歌曲
                break;
            case R.id.tv_clean:
                //清除历史记录  一下子全部删除
                UiUtils.toast("清除历史记录");
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("确定要删除全部历史记录？");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        mList.clear();


                        mMLvAdapter.notifyDataSetChanged();
                    }
                });
                builder.setNegativeButton("取消", null);
                builder.create().show();
                break;
        }
    }

    private SeachInfo toBean(String name) {
        SeachInfo info = new SeachInfo(name);
        return info;
    }


    class LvAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public SeachInfo getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = UiUtils.inflateView(R.layout.item_seachactivity);
                holder = new ViewHolder();
                holder.tv = (TextView) convertView.findViewById(R.id.tv_name);
                convertView.setTag(holder);

            } else {
                holder = (ViewHolder) convertView.getTag();

            }

            SeachInfo info = getItem(position);

            holder.tv.setText(info.getName());

            holder.tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UiUtils.toast("跳到开始播放歌曲页面");
                }
            });

            return convertView;
        }
    }

    static class ViewHolder {
        TextView tv;
    }
}
//搜索记录不用上传服务器 用本地数据库